﻿using System.Text;
using System.Web.Mvc;
using System.Linq;
using System.Collections.Generic;
using Avalon.Core.Data;
using System;
using Avalon.Core.Providers;

namespace Avalon.Core.MVC.Helpers
{
    public class DetailFormHelper : Avalon.Core.MVC.IViewRenderHelper
    {
        public MvcHtmlString Render(HtmlHelper htmlHelper)
        {
            //Create the string builder
            StringBuilder builder = new StringBuilder();

            //Get an instace of the meta data provider
            ModelMetadata modelMetadata = htmlHelper.GetModelMetadata();

            //Get the parent property
            ModelMetadata parentProperty = modelMetadata.Properties.FirstOrDefault(m => m.IsParentProperty());

            //Get the references
            IEnumerable<ModelMetadata> referenceProperties = modelMetadata.Properties.Where(m => m.IsEntityProperty() || m.IsViewModelProperty());

            //Get the collections
            IEnumerable<ModelMetadata> collectionProperties = modelMetadata.Properties.Where(m => m.IsCollectionProperty());

            //Get simple properties
            IEnumerable<ModelMetadata> simpleProperties = modelMetadata.Properties.Where(m => m.IsSimpleProperty());

            IEntityDescriptor descriptor = ProviderContainer.Current.EntityProvider.GetEntityDescriptor();
            if (descriptor != null) {
                builder.AppendFormat("<h1>{0}</h1>", descriptor.Name);
                builder.AppendFormat("<p>{0}</p>", descriptor.Description);
            }
            else {
                builder.AppendFormat("<h1>{0} details</h1>", modelMetadata.GetTitle());
            }
            builder.AppendFormat("<a href='{0}' class='btn'>Edit</a>", modelMetadata.GetEntityEditUrl());

            // The Parent Property
            if (parentProperty != null) {
                builder.AppendFormat("<a href='{0}' class='btn' style='margin:2px;'>{1}</a>", parentProperty.GetEntityDetailUrl(), parentProperty.GetTitle());
            }
            else {
                builder.AppendFormat("<a href='{0}' class='btn'>Back to list</a>", modelMetadata.GetEntityListUrl());
            }

            // The simple properties
            builder.AppendFormat("<table class='detail-form' width='100%'>{0}", Environment.NewLine);
            builder.AppendFormat("<col width='30%' minwidth='200px'/>");
            builder.AppendFormat("<col width='68%' />{0}", Environment.NewLine);
            foreach (var property in simpleProperties.Where(p => p.ShowForDisplay))
            {
                builder.AppendFormat("<tr class='detail-row'><td>{0}</td><td>{1}</td></tr>{2}", property.GetTitle(), property.Model, Environment.NewLine);
            }
            builder.AppendFormat("</table>");

            // the collection properties
            foreach (var property in collectionProperties.Where(p => p.ShowForDisplay && p.Model != null))
            {
                builder.AppendFormat("<div>", property.GetTitle());
                builder.AppendFormat(new GridHelper().Render(new ReferencedCollection((IEnumerable<IEntity>)property.Model) { AllowAdd = true, Parent = (IEntity)modelMetadata.Model }).ToString());
                builder.AppendFormat("</div>");
            }

            return new MvcHtmlString(builder.ToString());
        }
    }
}
